function PatternCheck(testString){
	this.testString = testString;
	this.patternHeader = /^#(#{0,5})(.*[^#])/;
	this.patternHeaderSetextFirst = /\n([^\n]+)\n={2,}\n/g;
	this.patternHeaderSetextSecond = /\n([^\n]+)\n-{2,}\n/g;
	this.blockquote = 0;
	this.patternCode = /\n`{3,}([^\n]*)((?:.|\n)*?)`{3,}/g;
	this.patternEmphasis = /\*\*(\*?.*?\*?)\*\*/g;
	this.patternItalic = /\*([^\*]+?)\*/g;
	this.patternHorizontal = /\n([-\*]) *\1 *\1( |\1)*\n/g;
	this.patternKeyOne = /`([^`\n]+)`/g;
	this.patternKeyTwo = /``([^\n]+?)``/g;
	this.patternImage = / /;
	this.patternLink = /\[([^\]]+)\]\(([^\ )]+)((?: +)(['"])(.*?)\4)?\)/g;
}

PatternCheck.prototype = {

	constructor: PatternCheck,
   
	escapeSymbol: function(origin){
		origin = origin.replace(/\\`/g, '&apos;');
		origin = origin.replace(/\\\*/g, '&stars;');
		origin = origin.replace(/\\>/g, '&gt;');
		origin = origin.replace(/\\</g, '&lt;');
		origin = origin.replace(/\\_/g, '&underline;');
		origin = origin.replace(/\\\[/g, '&lsquare;');
		origin = origin.replace(/\\\]/g, '&rsquare;');
		origin = origin.replace(/\\\(/g, '&lparenthese;');
		origin = origin.replace(/\\\)/g, '&rparenthese;');
		return origin;
	},

	unescapeSymbol: function(origin){
		var res;
		res = origin.replace(/&stars;/g, '*');
		res = res.replace(/&underline;/g, '_');
		res = res.replace(/&lsquare;/g, '[');
		res = res.replace(/&rsquare;/g, ']');
		res = res.replace(/&lparenthese;/g, '(');
		res = res.replace(/&rparenthese;/g, ')');
		return res;
	},

	wrapCode: function(){
		this.testString = '\n' + this.testString;
		this.testString = this.escapeSymbol(this.testString);
		this.testString = this.testString.replace(/\\`/g, '&lsquo;');
		var arr = this.testString.split(this.patternCode);
		var sum = (arr.length - 1) / 3;
		var x = 0;
		console.log(arr);
		var returnString = this.regTransform(arr[0] + '\n');
		while(x < sum){
			if(arr[x * 3 + 1]){
				returnString += '<pre><code class="hljs'+ arr[x*3+1] + '">' + arr[x*3+2] + '</code></pre>';
			} else {
				returnString += '<pre><code class="hljs">' + arr[x*3+2] + '</code></pre>';
			}
			returnString += this.regTransform('\n'+arr[x*3+3]+'\n');
			x ++;
		}
		this.testString = returnString;
	},

	regTransform: function(origin){
		// var reg = /^\n/;
		// console.log(reg.test(origin));
		origin = origin.replace(this.patternHorizontal, '\n<hr>');
		console.log(origin);
		origin = origin.replace(this.patternHeaderSetextFirst, '\n<h1>$1</h1>\n');
		origin = origin.replace(this.patternHeaderSetextSecond, '\n<h2>$1</h2>\n');
		var res = origin.split(/\n/g);
		if(!res[res.length - 1]){
			res.pop();
		}
		var x; 
		for(x in res){
			if(res[x][0] == '>'){
				this.blockquote = 1;
				res[x] = res[x].slice(1);
			}
			var flagPassage = 1;

			var matchResult = this.patternHeader.exec(res[x]);
			if(matchResult){
				console.log(res[x]);
				res[x] = '<h' + (matchResult[1].length + 1) + '>' + matchResult[2] + '</h' + (matchResult[1].length + 1) + '>';
				flagPassage = 0;
			} else{
				res[x] = '<p>' + res[x] + '</p>';
			}
			res[x] = res[x].replace(this.patternLink, '<a href="$2" title="$5">$1</a>');
			res[x] = res[x].replace(this.patternKeyTwo, '<code class="key">$1</code>');
			res[x] = res[x].replace(this.patternKeyOne, '<code class="key">$1</code>');
			// res[x] = res[x].replace(/\*\*([^\*].*?)\*\*/g, '<strong>$1</strong>');
			res[x] = res[x].replace(this.patternEmphasis, '<strong>$1</strong>');
			var resArray = res[x].split(/<strong>(.*?)<\/strong>/g);
			// console.log(resArray);
			for(y in resArray){
				resArray[y] = resArray[y].replace(this.patternItalic, '<em>$1</em>');
			}

			var z = 1;
			while(z < resArray.length){
				if(z % 2){
					resArray[z - 1] += '<strong>';
				} else {
					resArray[z - 1] += '</strong>';
				}
				z ++;
			}

			// console.log('after' + resArray);
			res[x] = resArray.join('');
			if(this.blockquote){
				res[x] = '<blockquote>' + res[x] + '</blockquote>';
				// console.log('blockquote');
			}
		}
		res = res.join('');
		res = this.unescapeSymbol(res);
		// res = res.replace(/&stars;/g, '*');
		// res = res.replace(/&underline;/g, '_');
		res = res.replace(/<\/blockquote><blockquote>/g, '');
		res = res.replace(/<\/p><p>/g, '<br>');
		res = res.replace(/<p><br>/g, '<p>');
		res = res.replace(/<br><\/p>/g, '</p>');
		// console.log('last: ' + res);
		return res;
	},

	getResult: function(){
		console.log('before: ' + this.testString);
		this.wrapCode();	
		console.log('after: ' + this.testString);
		return this.testString;
	}
}
